package com.ifgostudio.framework.repository.support;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.Serializable;

import javax.persistence.EntityManager;  
  
import org.springframework.data.jpa.repository.JpaRepository;  
import org.springframework.data.jpa.repository.support.JpaEntityInformation;  
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;  
import org.springframework.data.repository.core.RepositoryMetadata;  

/**
 * 基础Repository工厂类，生成自定义的Repository基类
 * @author Wangjk
 * @Version 1.0
 * @Date 2013-3-22
 */
public class BaseJpaRepositoryFactory extends JpaRepositoryFactory {  
  
    public BaseJpaRepositoryFactory(EntityManager entityManager) {  
        super(entityManager);  
        // TODO Auto-generated constructor stub  
    }  
    @Override  
    @SuppressWarnings("unchecked")  
    protected JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager em) {  
  
        JpaEntityInformation<Object, Serializable> entityMetadata = mock(JpaEntityInformation.class);  
        when(entityMetadata.getJavaType()).thenReturn((Class<Object>) metadata.getDomainType());  
        return new JpaRepositorySupport<Object, Serializable>(entityMetadata, em);  
    }  
  
    /* 
     * (non-Javadoc) 
     *  
     * @see 
     * org.springframework.data.repository.support.RepositoryFactorySupport# 
     * getRepositoryBaseClass() 
     */  
    @Override  
    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {  
  
        return JpaRepositorySupport.class;  
    }  
}  